package cn.schoolwow.quickdao.module.database.parent.flow.executor.common;

import cn.schoolwow.quickdao.domain.QuickDAOConfig;
import cn.schoolwow.quickdao.domain.entity.Entity;
import cn.schoolwow.quickdao.module.database.dml.common.domain.ManipulationOption;
import cn.schoolwow.quickdao.module.database.dml.instance.flow.update.flow.SetUpdateInstanceParameterFlow;
import cn.schoolwow.quickdao.module.database.dml.instance.flow.update.flow.SetUpdateInstanceStatementFlow;
import cn.schoolwow.quickdao.module.database.dml.instance.flow.update.flow.updateType.SetPropertyListByUniqueKeyFlow;
import cn.schoolwow.quickdao.module.database.parent.flow.executor.ExecuteUpdateConnectionFlow;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;

public class UpdateLogFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        QuickDAOConfig quickDAOConfig = flowContext.checkInstanceData(QuickDAOConfig.class);
        Object instance = flowContext.checkData("instance");
        Entity entity = quickDAOConfig.databaseContext.entityMap.get(instance.getClass().getName());
        if(null==entity){
            throw new IllegalArgumentException("日志类信息获取失败!日志类名:"+instance.getClass().getName());
        }

        flowContext.startFlow(new SetPropertyListByUniqueKeyFlow())
                .next(new SetUpdateInstanceStatementFlow())
                .next(new SetUpdateInstanceParameterFlow())
                .next(new ExecuteUpdateConnectionFlow())
                .putTemporaryData("name", "更新日志记录")
                .putTemporaryData("entity", entity)
                .putInstanceData(new ManipulationOption())
                .execute();
    }

    @Override
    public String name() {
        return "更新日志记录";
    }
}
